<?php

/**
 * Description of page_model
 *
 * @author iLyas
 */

class Page_model extends CI_Model{
    
    function create_page($user_id){
        
            $new_page_insert_data = array (
                'title_page'                    => $this->input->post('page_title'),
                'short_description_page'        => $this->input->post('short_description'),
                'description_page'              => $this->input->post('long_description'),
                'nbr_cases_page'                => $this->input->post('nbr_cases'),
                'prix_case_page'                => $this->input->post('prix_case'),
                'keywords_page'                 => $this->input->post('keywords'),
                'id_user_page'                  => $user_id,
            );
            $insert = $this->db->insert('page', $new_page_insert_data);
            return $insert;
    }
    
    function page_exists(){
        
        $this->db->where('title_page', $this->input->post('page_title'));
        $q = $this->db->get('page');
        
        return (bool) $q->num_rows;
    }
    
    function id_page_exists($id){
        $this->db->where('id_page', $id);
        $q = $this->db->get('page');
        
        return (bool) $q->num_rows;
    }
    
    function getUserPageInfo($id_user){
        $q = $this->db->query('SELECT * FROM page WHERE id_user_page = '.$id_user.' LIMIT 1');
        
        if($q->num_rows() > 0){
            foreach ($q->result() as $row){
                $data[] = $row;
            }
        }
        return $data;
    }
    
    function get_latest_pages($nbr){
        $q = $this->db->get('page', $nbr);
        foreach ($q->result() as $row){
            $data[] = $row;
        }
        return $data;
    }
    
    function getPageInfo($id_page){
        $q = $this->db->query('SELECT * FROM page WHERE id_page = '.$id_page.' LIMIT 1');
        
        if($q->num_rows() > 0){
            foreach ($q->result() as $row){
                $data[] = $row;
            }
        }
        return $data;
    }
    
    function pageBelongsTo($id_page){
        $q = $this->db->query('SELECT id_user_page FROM page WHERE id_page = '.$id_page.' LIMIT 1');
        
        if($q->num_rows() > 0){
            $row = $q->row();
            $data = $row->id_user_page;
        }
        return $data;
    }
    
    function get_user_pages($user_id){
        $this->db->where('id_user_page', $user_id);
        $q = $this->db->get('page');
        if($q->num_rows() > 0)
            $data = $q->result();
        
        return $data;        
    }
    
    function getPageStatistics($id_page){
        return $data;
    }
    
    function getPageDonations($id_page){
        return $data;
    }
}

?>
